﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public;

namespace QuanLyKhoHangVatTu
{
    public partial class ChiTietNhanVien : DevExpress.XtraEditors.XtraForm, IChildForm
    {
        NhanVien_BUS nhanvien_bus = new NhanVien_BUS();
        NhanVien_PB nhanvien_pb = new NhanVien_PB();
        Flag_PB flag_pb = new Flag_PB();
        public ChiTietNhanVien()
        {
            InitializeComponent();
        }

        private void ChiTietNhanVien_Load(object sender, EventArgs e)
        {
            Nap();
        }
        public void Nap()
        {
            gridNhanVien.DataSource = nhanvien_bus.LayDSNhanVien();
        }
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //NhanVien_PB.MaNhanVien = gridView1.GetRowCellValue(e.FocusedRowHandle, "MANHANVIEN").ToString();
            //NhanVien_PB.TenNhanVien = gridView1.GetRowCellValue(e.FocusedRowHandle, "TENNHANVIEN").ToString();
            //NhanVien_PB.GioiTinh = gridView1.GetRowCellValue(e.FocusedRowHandle, "GIOITINH").ToString();
            //NhanVien_PB.NgaySinh = Convert.ToDateTime(gridView1.GetRowCellValue(e.FocusedRowHandle, "NGAYSINH").ToString());
            //NhanVien_PB.MaSoThue = gridView1.GetRowCellValue(e.FocusedRowHandle, "MASOTHUE").ToString();
            //NhanVien_PB.CMND = gridView1.GetRowCellValue(e.FocusedRowHandle, "CMND").ToString();
            //NhanVien_PB.NgayCap = Convert.ToDateTime(gridView1.GetRowCellValue(e.FocusedRowHandle, "NGAYCAP").ToString());
            //NhanVien_PB.NoiCap = gridView1.GetRowCellValue(e.FocusedRowHandle, "NOICAP").ToString();
            //NhanVien_PB.ChucDanh = gridView1.GetRowCellValue(e.FocusedRowHandle, "CHUCDANH").ToString();
            //NhanVien_PB.DonVi = gridView1.GetRowCellValue(e.FocusedRowHandle, "DONVI").ToString();
            //NhanVien_PB.LuongThoaThuan = gridView1.GetRowCellValue(e.FocusedRowHandle, "LUONGTHOATHUAN").ToString();
            //NhanVien_PB.HeSoLuong = float.Parse(gridView1.GetRowCellValue(e.FocusedRowHandle, "HESOLUONG").ToString());
            //NhanVien_PB.TKNganHang = gridView1.GetRowCellValue(e.FocusedRowHandle, "TKNGANHANG").ToString();
            //NhanVien_PB.MoTaiNganHang = gridView1.GetRowCellValue(e.FocusedRowHandle, "MOTAINGANHANG").ToString();
            //NhanVien_PB.DiaChi = gridView1.GetRowCellValue(e.FocusedRowHandle, "DIACHI").ToString();
            //NhanVien_PB.DienThoai = gridView1.GetRowCellValue(e.FocusedRowHandle, "DIENTHOAI").ToString();
            //NhanVien_PB.Email = gridView1.GetRowCellValue(e.FocusedRowHandle, "EMAIL").ToString();
        }
        public void Them()
        {
            NhanVien nv = new NhanVien();
            nv.Tag = this;
            nv.ShowDialog();
            Nap();
        }

        public void Sua()
        {
            // lay gia tri dang chọn tren grd moi dung

            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;

            NhanVien nhanvien = new NhanVien(r["MANHANVIEN"].ToString(), r["TENNHANVIEN"].ToString(), r["GIOITINH"].ToString(), Convert.ToDateTime(r["NGAYSINH"]), r["MASOTHUE"].ToString(), r["CMND"].ToString(), Convert.ToDateTime(r["NGAYCAP"]), r["NOICAP"].ToString(), r["CHUCDANH"].ToString(), r["DONVI"].ToString(), Convert.ToDecimal(r["LUONGTHOATHUAN"]), Convert.ToDouble(r["HESOLUONG"]), r["TKNGANHANG"].ToString(), r["MOTAINGANHANG"].ToString(), r["DIACHI"].ToString(), r["DIENTHOAI"].ToString(), r["EMAIL"].ToString());

            nhanvien.Tag = this;
            nhanvien.ShowDialog();
            Nap();
        }
        public void Xoa()
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;
            if (XtraMessageBox.Show(string.Format("Bạn có chắc muốn xóa nhân viên <{0}>?"
                , r["MANHANVIEN"]), "Xác nhận"
                 , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            try
            {
                nhanvien_bus.XoaNhanVien(r["MANHANVIEN"].ToString());
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("conflicted with the REFERENCE constraint"))
                {
                    XtraMessageBox.Show("Không thể xóa vì có dữ liệu quan hệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            Nap();
        }

    }
}